package com.six.park.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;


public class Delete_pic {
	// 文件输入流所需方法
	static class MyExtFilter implements FilenameFilter {

		private String ext;

		MyExtFilter(String ext) {
			this.ext = ext;
		}

		@Override
		public boolean accept(File dir, String name) {
			return name.endsWith(ext);
		}
	}

	static class CompratorByLastModified implements Comparator<File> {
		public int compare(File f1, File f2) {
			long diff = f1.lastModified() - f2.lastModified();
			if (diff > 0)
				return -1;//倒序正序控制
			else if (diff == 0)
				return 0;
			else
				return 1;//倒序正序控制
		}
		public boolean equals(Object obj) {
			return true;
		}
	}

	public String delete() throws IOException {

		//本地文件夹的路径
		File file = new File("/root/six/carimg/in");
		File[] fs = file.listFiles();
		Arrays.sort(fs, new CompratorByLastModified());
		for (int i = 0; i < fs.length; i++) {
			System.out.println(fs[i].getName()+" "+new Date(fs[i].lastModified()).toLocaleString());
		}
		//获取最后一张图片的名字
		System.out.println(fs[0].getName()+" ");
		String car = fs[0].getName().toString();

		//本地文件夹的路径
		String path="/root/six/carimg/out";
		String path2="/root/six/carimg/in";
		System.out.println(car);
		File In = new File(path + "\\" + car);
		System.out.println(path + "\\" + car);
		File Out = new File(path2 + "\\" + car);
		System.out.println(path2 + "\\" + car);

		In.delete();
		Out.delete();
		System.out.println("删除成功");
		return "删除成功";

	}

}
